package com.wang.rabbitmqstudy.web;

import com.wang.rabbitmqstudy.config.PropertiesConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 基本示例，包含普通的直连交换机、主题交换机、广播交换机
 */
@Api(tags = "基本示例")
@RequestMapping("/basic")
@RestController
public class BasicController {
    @Autowired
    private RabbitMessagingTemplate rabbitMessagingTemplate;
    @Autowired
    private PropertiesConfig propertiesConfig;


    @ApiOperation(value = "直连交换机")
    @GetMapping("/direct")
    public String direct(){
        String payload = "direct";
        rabbitMessagingTemplate.convertAndSend(propertiesConfig.getDirectExchange(),propertiesConfig.getRedRoutingKey(),payload);
        return "direct done";
    }

    @ApiOperation(value = "主题交换机1")
    @GetMapping("/topic1")
    public String topic1(){
        String payload = "topic1";
        String routingKey = "blue.a";
        rabbitMessagingTemplate.convertAndSend(propertiesConfig.getTopicExchange(),routingKey,payload);
        return "topic1 done";
    }

    @ApiOperation(value = "主题交换机2")
    @GetMapping("/topic2")
    public String topic2(){
        String payload = "topic2";
        String routingKey = "yellow.abc";
        rabbitMessagingTemplate.convertAndSend(propertiesConfig.getTopicExchange(),routingKey,payload);
        return "topic2 done";
    }

    @ApiOperation(value = "广播交换机，无需routing Key")
    @GetMapping("/fanout")
    public String fanout(){
        String payload = "fanout";
        String routingKey = null;
        rabbitMessagingTemplate.convertAndSend(propertiesConfig.getFanoutExchange(),routingKey,payload);
        return "fanout done";
    }
}
